package com.yd.mvc.pc.couponController;

import com.yd.app.service.pc.couponService.CouponService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Coupon;
import com.yd.domain.business.UserCoupon;
import com.yd.domain.business.UserCouponSend;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.DicEnum;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券
 * Created by sunxianhui on 2018/3/9.
 */
@RestController
@RequestMapping(value = "/api/coupon")
public class CouponController extends BaseController {
    @Autowired
    CouponService couponService;

    /**
     * 添加
     * @param coupon
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Coupon addCoupon(@RequestBody Coupon coupon) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.COUPON_MANAGER);//验证权限
        }
        int count= couponService.addCoupon(coupon);
        if(count==1){
            return coupon;
        }else {
            throw new BusinessException(500, "添加优惠券信息失败");
        }
    }

    /**
     * 修改
     * @param coupon
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateCoupon(@RequestBody Coupon coupon) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.COUPON_MANAGER);//验证权限
        }
        couponService.updateCoupon(coupon);
    }

    /**
     * 删除
     * @param id
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public void deleteCoupon(@PathVariable("id") String id) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.COUPON_MANAGER);//验证权限
        }
        couponService.deleteCouponByid(id);
    }
    /**
     * 查询
     * @param id
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Coupon getCouponById(@PathVariable("id") String id) {
        Coupon coupon=couponService.getCouponById(id);
        return coupon;
    }

    /**
     * 根据条件分页查询
     * @param coupon
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getList/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getCouponList(@RequestBody Coupon coupon, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) throws BusinessException {
        PageModel pageModel = null;
        try {
            pageModel = couponService.getCouponList(coupon, pageIndex, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(500, "获取订单信息失败");
        }
        return pageModel;
    }

    /**
     * 发放优惠券
     * @param ucs
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/send/coupon", method = RequestMethod.POST)
    public void sendCouponToUser(@RequestBody UserCouponSend ucs) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.COUPON_MANAGER);//验证权限
        }
        couponService.sendCouponToUser(ucs);
    }
}
